<template>
	<view class="container">
		<view class="content">
			<view class="text">
				<image src="../../../static/partyBuilding/party/2.png"></image>
				微信扫码付款
			</view>
			<view id="qrcode"></view>
			
		</view>

	</view>
</template>

<script>
	import QRCode from 'qrcodejs2'
	import {
		getOrderFormById2
	} from '@/api/pay/index.js'
	export default {
		data() {
			return {
				codeUrl: '', //微信二维码
				orderFormId: '', //id
				// socketTask: null,
				timer: null
				// is_open_socket: false
			}
		},
		onLoad() {
			this.codeUrl = decodeURIComponent(this.$route.query.codeurl);
			this.orderFormId = decodeURIComponent(this.$route.query.orderFormId);


		},
		onShow() {
			this.$nextTick(() => {
				this.getQRCode(this.codeUrl)
				this.connectSocketInit()
			})
		},
		beforeDestroy() {
			clearInterval(this.timer)
		},
		methods: {
			getQRCode(codeUrl) {
				console.log(codeUrl)
				new QRCode('qrcode', {
					text: codeUrl,
					width: 256, //图像宽度
					height: 256, //图像高度
					colorDark: '#000000', //前景色
					colorLight: '#ffffff', //背景色
					margin: 0, //外边距
					correctLevel: QRCode.CorrectLevel
						.H //容错级别。属性值有：QRCode.CorrectLevel.L、QRCode.CorrectLevel.M、QRCode.CorrectLevel.Q、QRCode.CorrectLevel.H

				})
			},
			// 创建websocket链接
			connectSocketInit() {
				let that = this;
				that.timer = setInterval(() => {
					// console.log('123')
					getOrderFormById2(this.orderFormId).then(res => {
						const result = res.data;
						if (result.trade_state === "SUCCESS") {
							clearInterval(that.timer)
							uni.showModal({
								title: result.trade_state_desc,
								showCancel: false,
								success: (res) => {
									if (res.confirm) {
										uni.navigateBack()
									}
								}
							})
						}
					})
				}, 5000)
				// that.socketTask = uni.connectSocket({
				// 	url: `ws://192.168.13.127:8080/village/pay/getOrderFormById2?orderFormId=${this.orderFormId}`,
				// 	success(res) {
				// 		console.log('websocket链接成功！')
				// 	},
				// 	fail(err) {
				// 		console.log('报错',err)
				// 	}
				// });
				// that.socketTask.onOpen((res)=>{
				// 	console.log('websocket链接正常打开')
				// })
			},
		},
	}
</script>

<style scoped lang="scss">
	.container {
		width: 100vw;
		height: 90vh;
		display: flex;
		justify-content: center;
		align-items: center;
		text-align: center;
	}

	.content {
		
		.text {
			padding: 20rpx 0;
			font-size: 42rpx;
			display: flex;
			justify-content: center;
			image{
				width: 50rpx;
				height: 50rpx;
			}
		}
	}
</style>